<?php
namespace app\admin\command;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use wxpay\Vehicle;

Class Test extends Command
{
    protected function configure()
    {
        $this->setName('test')->setDescription('Here is the remark ');
    }

    protected function execute(Input $input, Output $output)
    {
        $models = new Vehicle();
        $time = time();
        $changpaiData = $models->changpai();
        $changpai = $changpaiData;
        foreach($changpai as $changpai_key => $changpai_val){
            if (!is_array($changpai_val) || empty($changpai_val))
            {
                echo 2;
                continue;
            }
            if (!empty($changpai_val['fullName']))
            {
                $changpai_id = \think\Db::name('vehicle')->insertGetId([
                    'pid' => 0,
                    'level' => 1,
                    'create_time' => $time,
                    'first' => $changpai_val['initLetter'],
                    'mergename' => $changpai_val['name'],
                    'flag' => 'nohot',
                    'logo' => $changpai_val['logo'],
                    'pids' => 0
                ]);
                $chexing = $models->chexing($changpai_val['fullName']);

                foreach($chexing as $chexing_k => $chexing_v)
                {
                    echo '车型';
                    $chexing_id = \think\Db::name('vehicle')->insertGetId([
                        'pid' => 0,
                        'level' => 1,
                        'create_time' => $time,
                        'first' => $changpai_val['initLetter'],
                        'mergename' => $changpai_val['name'],
                        'flag' => 'nohot',
                        'logo' => $changpai_val['logo'],
                        'pids' => 0
                    ]);
                    if (!empty($changpai_val['fullName'])){
                        $pailiang = $models->pailiang($chexing_v['code']);
                        echo '排量';
                        foreach ($pailiang as $pailiang_k => $pailiang_v)
                        {
                            if (!empty($pailiang_v))
                            {
                                echo '年份';
                                 $nianfen = $models->nianfen($chexing_v['code'], $pailiang_v);
                                 foreach ($nianfen as $nianfen_k => $nianfen_v){
                                     $nianfenData = \think\Db::name('vehicle')->where('level',3)->where('pid',$chexing_id)->select();
                                     if ($nianfenData){
                                        foreach ($nianfenData as $nianfenData_k => $nianfenData_v)
                                        {
                                            if ($nianfenData_v == $nianfen_v)
                                            {
                                                $nianfen_pid = $nianfen_v['id'];
                                            }else{
                                                $nianfen_pid = \think\Db::name('vehicle')->insertGetId([
                                                    'pid' => $chexing_id,
                                                    'level' => 3,
                                                    'create_time' => $time,
                                                    'first' => $changpai_val['initLetter'],
                                                    'mergename' => $nianfen_v,
                                                    'flag' => 'nohot',
                                                    'pids' => '0' . '-' . $changpai_id . '-' . $chexing_id
                                                ]);
                                            }
                                        }
                                     }else{
                                         $nianfen_pid = \think\Db::name('vehicle')->insertGetId([
                                             'pid' => $chexing_id,
                                             'level' => 3,
                                             'create_time' => $time,
                                             'first' => $changpai_val['initLetter'],
                                             'mergename' => $nianfen_v,
                                             'flag' => 'nohot',
                                             'pids' => '0' . '-' . $changpai_id . '-' . $chexing_id
                                         ]);
                                     }

                                     $pailiang_pid = \think\Db::name('vehicle')->insertGetId([
                                         'pid' => $nianfen_pid,
                                         'level' => 4,
                                         'create_time' => $time,
                                         'first' => str_replace('.','',$changpai_val['initLetter']),
                                         'mergename' => $pailiang_v,
                                         'flag' => 'nohot',
                                         'pids' => '0' . '-' . $changpai_id . '-' . $chexing_id . '-' . $nianfen_pid
                                     ]);

                                     $kuanxing = $models->kuanxing($chexing_v['code'], $pailiang_v, $nianfen_v);

                                     if (empty($kuanxing))
                                     {
                                         echo 'bucu';
                                         continue;
                                     }

                                     foreach ($kuanxing as $kuanxing_k => $kuanxing_v) {
                                         $id = \think\Db::name('vehicle')->insertGetId([
                                             'pid' => $pailiang_pid,
                                             'level' => 5,
                                             'create_time' => $time,
                                             'first' => $changpai_val['initLetter'],
                                             'mergename' => $kuanxing_v['displayName'],
                                             'flag' => 'nohot',
                                             'pids' => '0' . '-' . $changpai_id . '-' . $chexing_id . '-' . $nianfen_pid . '-' . $pailiang_pid
                                         ]);
                                     }

                                 }
                            }
                        }
                    }
                }
            }
        }
    }
}